<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\AdminUser as AdminUserModel;
use think\Db;
use think\Env;
use think\Exception;

/**
 * 后台首页
 * Class Index
 * @package app\admin\controller
 */
class Index extends AdminBase
{
    protected function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 首页
     * @return mixed
     */
    public function index()
    {
      	$model = new AdminUserModel();
        $data = $model->get(['id'=>session("admin_id")]);
        return $this->fetch('index', [
          'data'=>$data,
          "sn" => lock(session("admin_id"))
          ]);
    }

    /**
     * 下载文件
     */
    public function down()
    {
        $id = session("admin_id");
        $info = $info = Db::table("os_shb")->where("uid",$id)->find();;
        $paths = [
          "doctor_pic",
          "kehu_pic",
          "yy_pic",
          "yyhjtp"
        ];
        foreach($paths as $val){
          $path = $info[$val];
          if(!file_exists(ROOT_PATH."/public".$path)){
            if("doctor_pic" == $val){
              $msg = "医生头像图片";
            }elseif("kehu_pic" == $val){
              $msg = "客服头像图片";
            }elseif("yy_pic" == $val){
              $msg = "引导页大图";
            }else{
              $msg = "环境图";
            }
            $this->error("请先配置升话宝中的".$msg,"shb/index");
          }
        }
        
        $options = new \ZipStream\Option\Archive();
        $options->setSendHttpHeaders(true);
        $zip = new \ZipStream\ZipStream('shb.zip', $options);


        foreach($paths as $val){
          $path = $info[$val];
          $doctor_pic = pathinfo( $path );
          $zip->addFileFromPath( $doctor_pic["basename"], ROOT_PATH."/public".$path);
          $info[$val] =  $doctor_pic["basename"];
        }
        $zip->addFileFromPath( "wechat.png", ROOT_PATH."/public/down/wechat.png");
        $zip->addFileFromPath( "wechat.gif", ROOT_PATH."/public/down/wechat.gif");
        $info["sn"] = lock($id);
        $info["duihua_arr"] = json_decode($info["duihua_arr"],true);
        if (!$info["duihua_arr"]){
            $info["duihua_arr"] = [];
        }
        preg_match("/[A-Za-z]*/",$info["swt_id"],$pre);
        if (!isset($pre[0]) || empty($pre[0])){
            exit("/** error swt_id */");
        }else{
            $pre = strtolower($pre[0]);
        }
        $info["pre"] = $pre;;
        $index = $this->fetch("down/index",$info);
        $zip->addFile('index.html', $index);

        # finish the zip stream
        $zip->finish();
  }
}
